const { CleanWebpackPlugin } = require('clean-webpack-plugin')
const webpack = require('webpack');
const paths = require('./paths')

module.exports = {
  mode: 'production',
  devtool: false,

  // Where webpack looks to start building the bundle
  entry: {
    imlib: { import: paths.src + '/lib/network.js' },
  },

  // Where webpack outputs the assets and bundles
  output: {
    path: paths.build_lib,
    filename: 'js/[name].[contenthash].bundle.js',
  },

  // Customize the webpack build process
  plugins: [
    // Removes/cleans build folders and unused assets when rebuilding
    new CleanWebpackPlugin(),

    new webpack.ProvidePlugin({
      process: 'process/browser',
      Buffer: ['buffer', 'Buffer']
    }),
  ],

  // Determine how modules within the project are treated
  module: {
    rules: [
      // JavaScript: Use Babel to transpile JavaScript files
      { test: /\.js$/, use: ['babel-loader'] },
    ],
  },

  resolve: {
    modules: [paths.src, 'node_modules'],
    extensions: ['.js', '.jsx', '.json'],
    alias: {
      '@': paths.src,
    },
    fallback: {
      path: require.resolve('path-browserify'),
      stream: require.resolve('stream-browserify'),
      crypto: require.resolve('crypto-browserify'),
      "process/browser": require.resolve("process/browser"),
      assert: require.resolve('assert/'),
    },
  },
  performance: {
    hints: false,
    maxEntrypointSize: 512000,
    maxAssetSize: 512000,
  },
}
